import axios from "axios"
import type { RouteRecordNormalized } from "vue-router"
import userHttpRequest from "@/db/user"

// 类型
export interface LoginData {
  username: string
  password: string
}
export interface RegisterData {
  username: string
  password: string
  rePassword: string
  captcha: string
}
export enum OperationType {
  register = "register",
  resetPassword = "resetPassword"
}
export interface CaptchaData {
  email: string
  operationType: OperationType
}
export interface LoginRes {
  token: string
}

// 登录
export function login(data: LoginData) {
  return userHttpRequest.login<LoginRes>(data)
}

// 获取用户信息
export function getUserInfo() {
  return userHttpRequest.getUserInfo<any>()
}

// 注册
export function register(data: RegisterData) {
  return userHttpRequest.register<null>(data)
}

// 发送邮箱验证码
export function sendCaptcha(data: CaptchaData) {
  return userHttpRequest.sendCaptcha<null>(data)
}

// 退出登录
export function logout() {
  return axios.post<LoginRes>("/user/logout")
}

// 获取菜单列表(菜单由服务端生成时启用)
export function getMenuList() {
  return axios.post<RouteRecordNormalized[]>("/user/menu")
}
